<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

class Login extends NotORM {

	protected $fields='id,user_nickname,avatar,avatar_thumb,sex,signature,coin,consumption,birthday,user_status,login_type,last_login_time,goodnums,badnums,isauth,votestotal,is_firstlogin,isauthor_auth';

	/* 会员登录 */   	
    public function userLogin($where) {

		$info=\PhalApi\DI()->notorm->user
				->select($this->fields)
				->where('user_type=2')
                ->where($where)
                ->order('id desc')
				->fetchOne();

		if($info){
			\PhalApi\DI()->notorm->user
				->where(['id'=>$info['id']])
				->update(['is_firstlogin'=>0]);

			$info['is_firstlogin']='0';
		}
                
        return $info; 
    }	
	
	/* 会员注册 */
    public function userReg($data=[]) {
        
        $nowtime=time();
        $user_pass='live1v1'.$nowtime;
        $user_pass=\App\setPass($user_pass);
        
        $nums = [1,2];
        $num = array_rand($nums,1);
        if($num == 1){
            $img = mt_rand(1,200);
            $avatar = 'man/'.$img.'.png';
            $avatar_thumb = 'man/'.$img.'.png';
        }else{
            $img = mt_rand(1,200);
            $avatar = 'woman/'.$img.'.png';
            $avatar_thumb = 'woman/'.$img.'.png';
        }
        
        /*$avatar='/default.png';
        $avatar_thumb='/default_thumb.png';*/

        $default=array(
            'user_pass' =>$user_pass,
            // 'signature' =>\PhalApi\T('这家伙很懒，什么都没留下'),
            'signature' =>\PhalApi\T(''),
            'avatar' =>$avatar,
            'avatar_thumb' =>$avatar_thumb,
            'last_login_ip' =>$_SERVER['REMOTE_ADDR'],
            'create_time' => $nowtime,
            'user_status' => 1,
            "user_type"=>2,//会员
            "is_firstlogin"=>1,
            "isauth"=> 0,
            "isauthor_auth"=>1,
            "isvoice"=>1,
            "isvideo"=>1,
        );
        
        $insert=array_merge($default,$data);
        
            
		$rs=\PhalApi\DI()->notorm->user->insert($insert);
        
        $id=$rs['id'];
        $author=array(
            'uid' =>$id,
            'thumb' =>'',
            'backwall' =>'',
            'video' =>'',
            'video_thumb' =>'',
            'status'=>1
        );
//        $auth=array(
//            'uid' =>$id,
//            'cardno' =>'',
//            'name' =>'',
//            'mobile' =>'',
//            'reason' =>'',
//            'status'=>1
//        );
        $rs1=\PhalApi\DI()->notorm->author_auth->insert($author);
//        $rs2=\PhalApi\DI()->notorm->user_auth->insert($auth);
        $info=\PhalApi\DI()->notorm->user
				->select($this->fields)
				->where('id=?',$id)
				->fetchOne();
                
		return $info;
    }
		

	/* 更新token 登陆信息 */
    public function updateToken($uid,$token,$data=array()) {
        
        $nowtime=time();
		$expiretime=$nowtime+60*60*24*150;

		\PhalApi\DI()->notorm->user
			->where('id=?',$uid)
			->update(array('last_login_time' => $nowtime, "last_login_ip"=>$_SERVER['REMOTE_ADDR'] ));

		$token_info=array(
			'user_id'=>$uid,
			'token'=>$token,
			'expire_time'=>$expiretime,
			'create_time'=>$nowtime,
		);
        $isexist=\PhalApi\DI()->notorm->user_token
			->where('user_id=?',$uid)
			->update( $token_info );
        if(!$isexist){
            \PhalApi\DI()->notorm->user_token
                ->insert( $token_info );
        }
		
		\App\setCache("token_".$uid,$token_info);		
        
		return 1;
    }	
	
	
	 //获取注销账号条件
    public function getCancelCondition($uid){

    	$res=array('list'=>array(),'can_cancel'=>'0');

    	$list=array(
    		'0'=>array(
    				'title'=>\PhalApi\T('1、账号内无大额未消费或未提现的财产'),
    				'content'=>\PhalApi\T('你账号内无未结清的欠款、资金和虚拟权益；注销后，账户中的虚拟权益等将作废无法恢复。'),
    				'is_ok'=>'0'
    			),
    	);

    	//获取用户的映票、钻石、余额
    	$userinfo=\PhalApi\DI()->notorm->user->where("id=?",$uid)->select("coin,votes")->fetchOne();
		
		//查询用户有没有提现中的金额
    	$record_count=\PhalApi\DI()->notorm->cash_record->where("uid=? and status=0",$uid)->count();
    	//钻石小于100，映票小于100，余额为0
    	if($userinfo['coin']<100&&$userinfo['votes']<100&&$record_count==0){
    		$list[0]['is_ok']='1';
			$res['can_cancel']='1';
    	}


    	$res['list']=$list;

    	return $res;

    }

    //注销账号
    public function cancelAccount($uid){
    	$condition=$this->getCancelCondition($uid);

    	if(!$condition['can_cancel']){
    		return 1001;
    	}
  	
    	$now=time();
		//修改用户昵称
		$user_info=array(
			'user_nickname'=>\PhalApi\T('用户已注销'),
			'avatar'=>'/default.png',
			'avatar_thumb'=>'/default_thumb.png',
			'user_status'=>'3',
			'online'=>'0',
			'isauth'=>'0',
			'isauthor_auth'=>'0',
		);
		
		\PhalApi\DI()->notorm->user->where("id=?",$uid)->update($user_info);	
		//删除认证信息
		\PhalApi\DI()->notorm->user_auth->where("uid=?",$uid)->delete();	 
		//删除主播认证信息
		\PhalApi\DI()->notorm->author_auth->where("uid=?",$uid)->delete();	
		//相册改为下架
		\PhalApi\DI()->notorm->photo->where("uid=?",$uid)->update(array('status'=>3));
		//上架的视频改为下架
		\PhalApi\DI()->notorm->video->where("uid=?",$uid)->update(array('status'=>3));
		//未审核的动态拒绝
		\PhalApi\DI()->notorm->dynamics->where("uid=? and status=0",$uid)->update(array('status'=>2));
		//已经审核通过的动态下架
		\PhalApi\DI()->notorm->dynamics->where("uid=? and status=1 and isdel=0",$uid)->update(array('isdel'=>1));

		$info=\PhalApi\DI()->notorm->user
					->select($this->fields)
					->where('id=? and user_type="2"',$uid)
					->fetchOne();

		if($info){
            \App\hMSet("userinfo_".$uid,$info);
        }


		return 1;

    }

}
